//
//  PosterView.m
//  MyMovie
//
//  Created by Weiwei on 16/2/29.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "PosterView.h"
#import "PosterCollectionView.h"
#import "IndexCollectionView.h"
#import "HomeModel.h"
@interface PosterView(){
    UIView* _headerView;//下拉海报
    UIView* _maskView;//遮挡试图
    UILabel* _titleLable;//标签
    PosterCollectionView* _posterVC;//大海报试图
    IndexCollectionView* _indexView;//小海报试图
}

@end
@implementation PosterView


#pragma -mark步骤一封装一个海报试图
-(id)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //3.封装底部的lable篮
        [self createBottomView];
#pragma -mark创建大海报试图
        [self createBigPosterView];
        
        //2.封装灯光试图
        [self createLight];
    
        //4.遮挡视图
        [self createMaskView];
        
        //1.封装下拉的小海报试图
        [self createHeaderView];

        [self createKVO];
        
    }
    
    return self;
    
}

#pragma -mark KVO监听同步
-(void)createKVO{
             
    //给某个对象建立一个KVO观察者
    [_posterVC addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    [_indexView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
    
}
//观察中心
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
   
    if ([keyPath isEqualToString:@"currentIndex"]) {
        
        //2.获得当前改编后的index值
       NSInteger newValue = [[change objectForKey:@"new"] integerValue];
        
        
        //1.判断currentIndex改变的是不是小海报试图
        if ([object isKindOfClass:[IndexCollectionView  class]]&&_posterVC.currentIndex != newValue) {
            
            //拼出要滑动的cell的indexPath
            NSIndexPath* indexP = [NSIndexPath indexPathForItem:newValue inSection:0];
            
            [_posterVC scrollToItemAtIndexPath:indexP atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
            
            _posterVC.currentIndex = newValue;
            
            
        }else if([object isKindOfClass:[PosterCollectionView class]]&&_indexView.currentIndex != newValue){
            //改变的是大海报试图
       
            
            NSIndexPath* path = [NSIndexPath indexPathForItem:newValue inSection:0];
            
            [_indexView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
            
            _indexView.currentIndex = newValue;
            
            
        }
        //----------给底部titleLable复制--------
        
        
        //获得当前的数据
        
        HomeModel* model  = [_dataList objectAtIndex:newValue];
        _titleLable.text = model.titleCn;
        
        
    }
    
    
    
    
    
    
    
    
}









//1.封装下拉海报试图
-(void)createHeaderView{
    
    //选取一个UIView作为下拉试图的父视图
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, -120, KScreenW, 150)];
    _headerView.backgroundColor = [UIColor clearColor];
    [self addSubview:_headerView];
    
    //增加一个imageView
    UIImageView* imageView = [[UIImageView alloc]initWithFrame:_headerView.bounds];
    [_headerView addSubview:imageView];
    imageView.image = [[UIImage imageNamed:@"indexBG_home@2x"]stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    
    //创建一个button
    UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(KScreenW/2 -15, 125+5, 30, 20)];
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(pullDown:) forControlEvents:UIControlEventTouchUpInside];
    
    [_headerView addSubview:button];
#pragma -mark 增加小海报试图
    [self createIndexPosterView];
    
}

-(void)createIndexPosterView{
    
    _indexView = [[IndexCollectionView alloc]initWithFrame:CGRectMake(0, 0, _headerView.frame.size.width, 120)];
    [_headerView addSubview:_indexView];
    
}





-(void)pullDown:(UIButton*)button{
    button.selected = !button.selected;
    
    if (button.selected == YES) {
        
        
        [UIView animateWithDuration:.3 animations:^{
            _headerView.transform = CGAffineTransformMakeTranslation(0, 120);
            //因为没给图片，设置button的旋转
            button.transform = CGAffineTransformMakeRotation(M_PI);
            _maskView.hidden = NO;
            
        }];
    
    }else{
        
        
        [UIView animateWithDuration:.3 animations:^{
            _headerView.transform =CGAffineTransformIdentity;
            
            button.transform = CGAffineTransformIdentity;
            _maskView.hidden =YES;
            
        }];
        
        
        
    }

}

//封装灯光视图
-(void)createLight{
    
    //1.设置灯光拦
    UIImageView* bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, KScreenW, 20)];
    bgView.image = [UIImage imageNamed:@"pic_title_bg.9"];
    [self addSubview:bgView];
    
    //2.创建一个右边的灯光imageView
    UIImageView* rightView = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenW -124, 10, 124, 204)];
    rightView.image = [UIImage imageNamed:@"light"];
    [self addSubview:rightView];
    
    //3.左边的
    UIImageView* leftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 124, 204)];
    leftView.image = [UIImage imageNamed:@"light"];
    [self addSubview:leftView];
    
}
//3.
-(void)createBottomView{
    
    _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 35 -64 -49, KScreenW, 35)];
    
    _titleLable.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"poster_title_home"]];
    _titleLable.textColor = [UIColor whiteColor];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    _titleLable.font =[UIFont systemFontOfSize:22];
//    _titleLable.text = @"小杨的秘密";
    
    
    
    [self addSubview:_titleLable];
    
    
}
//4.遮挡试图
-(void)createMaskView{
    
     _maskView = [[UIView alloc]initWithFrame:self.bounds];
    _maskView.backgroundColor = [UIColor blackColor];
    
    _maskView.hidden = YES;
    _maskView.alpha = .3;
    
    [self addSubview:_maskView];
    
    
    
    
}

#pragma -mark 创建大海报试图
-(void)createBigPosterView{
   
    _posterVC = [[PosterCollectionView alloc]initWithFrame:CGRectMake(0, 30, KScreenW, _titleLable.frame.origin.y - 30)];
    [self addSubview:_posterVC];
    
    
}


//--------------------------
#pragma -mark 当海报试图得到数据的时候，进行对下面的子时图分发数据
-(void)setDataList:(NSArray *)dataList{
    _dataList = dataList;//自己存一份
    
    //给大海报试图collectionView 发一份数据
    _posterVC.dataList = self.dataList;
    
    //小海报视图数据
    _indexView.dataList = self.dataList;
#pragma -mark 默认第一次情况
    HomeModel* model = [_dataList objectAtIndex:0];
    _titleLable.text = model.titleCn;
    
    
}


@end
